package flyshoes.controller;

import flyshoes.mybatis.entity.Result;
import flyshoes.mybatis.entity.ShoppingCart;
import flyshoes.service.CartServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequiredArgsConstructor
@RequestMapping("/cart")
public class CartServiceController {

    private final CartServiceImpl cartService;
    @GetMapping("/getCart")//查找购物车
    public Result getCart(){
        return cartService.getCart();
    }
    @PostMapping("/addCart")//增加商品到购物车
    public Result addCart(@RequestBody ShoppingCart cart){
        return cartService.addCart(cart);
    }
    @DeleteMapping("/{productId}/removeShoesInCart") //通过productId移除一个商品
    public Result removeShoesInCart(@PathVariable Integer productId){
        return cartService.removeShoesInCart(productId);
    }
    @DeleteMapping("/removeByCartIds") //通过多个cartId,移除多个商品
    public Result removeByCartIds(@RequestBody List<Integer> cartIds){
        return cartService.removeByCartIds(cartIds);
    }
    @PatchMapping("/{productId}/{quantity}/updateQuantity")
    public Result updateQuantity(@PathVariable Integer productId,@PathVariable Integer quantity){
        if (quantity <= 0) {
            return Result.fail("数量必须大于零");
        }
        return cartService.updateQuantity(productId,quantity);
    }
}
